import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.v85.runtime.model.ExceptionDetails;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class auto {
    ChromeDriver driver=new ChromeDriver();
    //显示等待
    public  void  ait(){
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
        driver.findElement(By.cssSelector("#su")).click();
       // driver.findElement(By.cssSelector("#\\31  > div > div.header-left_1BntJ > div.c-color-t.left-title_3lM8p.c-line-clamp1"));
        WebDriverWait foo=new WebDriverWait(driver,Duration.ofMillis(30));
        foo.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\32  > div > div > div:nth-child(3) > div.c-span9.c-span-last.main-info_4Q_kj > div:nth-child(2) > div")));
        String fo1=driver.findElement(By.cssSelector("#\\32  > div > div > div:nth-child(3) > div.c-span9.c-span-last.main-info_4Q_kj > div:nth-child(2) > div")).getText();
        System.out.println(fo1);
    }
    //清空加上隐式等待
    public  void clear() throws InterruptedException {
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#kw")).sendKeys("卿卿日常");
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#kw")).clear();
        driver.findElement(By.cssSelector("#kw")).sendKeys("向往的生活");
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#su")).click();
        //隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofMillis(20));
        driver.findElement(By.cssSelector("#\\31  > div > h3 > a"));
    }
    //获取文本消息
    public  void gettext(){
        driver.get("https://www.baidu.com");
        String text=driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a/span[2]")).getText();
        System.out.println("结果是："+text);
    }
    //输入并提交
    public  void submit() throws InterruptedException {
        //输入
        driver.findElement(By.cssSelector("#kw")).sendKeys("卿卿日常");
        //强制休眠3秒钟
        Thread.sleep(3000);
        //找到百度一下并提交
        driver.findElement(By.cssSelector("#su")).click();
    }
    public  void start(){
        driver.get("https://www.baidu.com");
    }
    //释放驱动器，关闭浏览器
    public  void end() throws InterruptedException {
        Thread.sleep(3000);
        driver.quit();
    }
}
